<template>
    <div class="card">
        <div class="card-body">
            <div class="row justify-content-md-center">
                <div class="col-3">
                    <form @submit.prevent="register">
                        <div class="mb-3">
                            <label class="form-label" for="username">用户名</label>
                            <input type="text" class="form-control" v-model="username" id="username">
                        </div>
                        <div class="mb-3">
                            <label class="form-label" for="phone_number">联系方式</label>
                            <input type="text" class="form-control" v-model="phone_number" id="phone_number">
                        </div>
                        <div class="mb-3">
                            <label class="form-label" for="phone_number">收货地址</label>
                            <input type="text" class="form-control" v-model="consumer_address" id="phone_number">
                        </div>
                        <div class="mb-3">
                            <label class="form-label" for="password">密码</label>
                            <input type="password" class="form-control" v-model="password" id="password">
                        </div>
                        <div class="mb-3">
                            <label class="form-label" for="password_confirm">确认密码</label>
                            <input type="password" class="form-control" v-model="password_confirm" id="password_confirm">
                        </div>
                        <div class="error_message">{{ error_message }}</div>
                        <button type="submit" class="btn btn-primary">注册</button>
                    </form>
                </div>
            </div>
        </div>
    </div>
</template>

<script>
import { ref } from 'vue';
import { useStore } from 'vuex';
import { useRouter } from 'vue-router';
import $ from 'jquery';
export default {
    name: "RegisterView",
    setup() {
        const store = useStore();
        const username = ref('');
        const password = ref('');
        const phone_number = ref('');
        const consumer_address = ref('');
        const password_confirm = ref('');
        const error_message = ref('');
        const router = useRouter();
        const register = () => {
            error_message.value = "";
            $.ajax({
                url: "http://47.121.28.202:8000/settings/register/",
                type: "POST",
                data: {
                    username: username.value,
                    password: password.value,
                    phone_number: phone_number.value,
                    consumer_address: consumer_address.value,
                    password_confirm: password_confirm.value,
                },
                success(resp) {
                    if (resp.result === "success") {
                        store.dispatch("login", {
                            username: username.value,
                            password: password.value,
                            success() {
                                router.push({ name: 'home' });
                            },
                            error() {
                                error_message.value = "系统异常，请稍后重试";
                            }
                        })
                    } else {
                        error_message.value = resp.result;
                    }
                }
            })
        }
        return {
            username,
            password,
            error_message,
            phone_number,
            password_confirm,
            consumer_address,
            register
        }
    }
}
</script>

<style scoped>
.btn-primary {
    width: 100%;
}
.error_message {
    color: red;
}
</style>